package com.sg.hms.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.sg.hms.Model.Applicationer;
import com.sg.hms.Model.Department2;
import com.sg.hms.Model.Employee2;
import com.sg.hms.Service.MoneyApplicationService;
import com.sg.hms.Service.SalaryService;
import com.sg.hms.ServiceImpl.MoneyApplicationServiceImpl;
import com.sg.hms.ServiceImpl.SalaryServiceImpl;
/**
 * 
 * @author lubinghan
 *
 */
public class ShowDepartSpendController extends HttpServlet{
	MoneyApplicationService mService = new MoneyApplicationServiceImpl();
	SalaryService sService = new SalaryServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		resp.setCharacterEncoding("utf-8");
		req.setCharacterEncoding("utf-8");
		PrintWriter pw = resp.getWriter();
		//获取数据展示条数和页请求参数
		int page = Integer.parseInt(req.getParameter("page"));
		int pageSize = Integer.parseInt(req.getParameter("rows"));
		int pageStart = (page-1)*pageSize;

		String dateStr = req.getParameter("date");//获得参数日期
		
		if(dateStr==null||dateStr==""){//加载财务表示要显示的数据
			
			int total = mService.selectMoneyByNow();//各部门支出的数据条数
			JSONObject spends = mService.selectMoneyByPage(pageStart, pageSize,total);
			pw.write(spends.toString());
		}else{
			//截取日期
			String datestr1 = dateStr.substring(0, 4);
			String dateStr2 = dateStr.substring(5,7);
			String date = datestr1+dateStr2;
			JSONObject departSpendInfo = mService.selectMoneyByDate(pageStart, pageSize, date);
			pw.write(departSpendInfo.toString());
		}

	}
}
